package com.lhf.springcloud.service.impl;

import com.lhf.springcloud.DAO.TeacherCompleteDataMapper;
import com.lhf.springcloud.DAO.TeacherDao;
import com.lhf.springcloud.Entity.teacher.TeacherData;
import com.lhf.springcloud.service.TeacherCompleteDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class TeacherCompleteDataServiceImpl implements TeacherCompleteDataService {

    @Autowired
    private TeacherCompleteDataMapper teacherCompleteDataMapper;

    @Autowired
    private TeacherDao teacherDao;
    @Override
    public Map findAllTeacherBaseData(Integer startPage,Integer endPage) {
        Map map = new HashMap<>();
        List<TeacherData> allTeacherBaseData = teacherCompleteDataMapper.findAllTeacherBaseData(startPage,endPage);
        int count = teacherCompleteDataMapper.account();
        map.put("data",allTeacherBaseData);
        map.put("count",count);
        return map;
    }

    @Override
    public List<TeacherData> findIdCard(String idCard) {
        List<TeacherData> oneTeacherData = teacherCompleteDataMapper.findByIdCard(idCard);
        return oneTeacherData;
    }

    @Override
    public void updateTeaherData(String value, String id, String field) {
        teacherCompleteDataMapper.updateTeacherData(value,id,field);
        //拿到修改对象的数据
        TeacherData byId = teacherCompleteDataMapper.findById(id);
        //判断是否存在
        List<TeacherData> teacherByIdCard = teacherDao.findTeacherByIdCard(byId.getIdCard());
        if(teacherByIdCard.size() == 0){
            teacherCompleteDataMapper.updateTeacherStatus("false1",id);
        }else if (teacherByIdCard.size() == 1){
            if (teacherByIdCard.get(0).getTeacherName().equals(byId.getTeacherName())){
                teacherCompleteDataMapper.updateTeacherStatus("false2",id);
            }else {
                teacherCompleteDataMapper.updateTeacherStatus(null,id);
            }
        }
    }

    @Override
    public void insert(TeacherData teacherData) {
        Integer s = 202042;
        String ss = "511620199503";
        String idCard = ss+ s.toString();
        teacherData.setIdCard(idCard);
        teacherData.setTeacherName("测试数据");
        teacherData.setGender("女");
        teacherData.setAcademicDegree("硕士");
        teacherData.setPosition("教师");
        teacherData.setCoursesOffered("英语");//主授课程
        teacherCompleteDataMapper.insert(teacherData);
    }

    @Override
    public String addCourse(List<TeacherData> list1) {
        for (int i =0;i<list1.size();i++){
            List<TeacherData> teacherByIdCard = teacherDao.findTeacherByIdCard(list1.get(i).getIdCard());
            if (teacherByIdCard.size()==0){
                list1.get(i).setInfoJudge("false1");
            }else if (teacherByIdCard.size()>1){
                return "请联系管理员，技术数据重复!";
            }else if (teacherByIdCard.size()==1){
                if (teacherByIdCard.get(0).getTeacherName().equals(list1.get(i).getTeacherName())){
                    list1.get(i).setInfoJudge("false2");
                }
            }

        }
        for (int i =0;i<list1.size();i++){
            teacherCompleteDataMapper.insert(list1.get(i));
        }
       return "完成操作！";
    }
}
